/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.miscellaneous.instrument;

import java.awt.BorderLayout;
import java.awt.Color;
import java.util.Date;
import java.util.List;

import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;

import twaver.AlarmSeverity;
import twaver.ElementAttribute;
import twaver.ElementAttributeVisibleFilter;
import twaver.Link;
import twaver.Node;
import twaver.TDataBox;
import twaver.TUIManager;
import twaver.TWaverConst;
import twaver.TWaverUtil;
import twaver.Text;
import twaver.network.TNetwork;
import twaver.network.background.ColorBackground;
import twaver.table.TPropertySheet;
import twaver.table.TPropertySheetPane;
import twaver.tree.TTree;
import demo.DemoPane;
import demo.network.miscellaneous.instrument.attachment.KPIAttachment;
import demo.network.miscellaneous.instrument.attachment.ThermometerAttachment;
import demo.network.miscellaneous.instrument.element.Clock;
import demo.network.miscellaneous.instrument.element.CustomUINode;
import demo.network.miscellaneous.instrument.element.Thermometer;

public class InstrumentDemo extends DemoPane {
	
	static{
        TUIManager.registerAttachment("Thermometer", ThermometerAttachment.class);
        TUIManager.registerAttachment("KPI", KPIAttachment.class);
        TUIManager.registerAttachment("dial", DialAttachment.class);		
	}
	
	private TDataBox box = new TDataBox();
	private TNetwork network = new TNetwork(box);
	private TTree tree = new TTree(box);
	private TPropertySheet sheet = new TPropertySheet(box);
    
	private boolean flag = false;
	private int step = 5;
	private int step2 = 0;
	private double angle = 0;
	
	private Thermometer thermometer = new Thermometer();
	private Clock clock = new Clock();
	private Node node1 = new CustomUINode();
	private Node node2 = new CustomUINode();
	private Link link = new Link(node1, node2);
	private Text dial = new Text();
	
	public InstrumentDemo(){
		ColorBackground background = new ColorBackground(new Color(53, 53, 153));
		background.setGradient(true);
		box.setBackground(background);
		
        sheet.setElementAttributeVisibleFilter(new ElementAttributeVisibleFilter(){
			public boolean isVisible(ElementAttribute attribute) {
				List list = attribute.getCategoryNames();
				if(list != null && "system".equals(list.get(0))){
					return false;
				}
				return true;
			}
        });
        
        JSplitPane leftSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT, new JScrollPane(tree), new TPropertySheetPane(sheet));
        JSplitPane rootSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftSplit, network);
        leftSplit.setDividerLocation(300);
        leftSplit.setContinuousLayout(true);
        rootSplit.setDividerLocation(240);
        rootSplit.setContinuousLayout(true);
        this.add(rootSplit, BorderLayout.CENTER);
		
		thermometer.setLocation(250, 180);
		box.addElement(thermometer);

		clock.setLocation(350, 100);
		box.addElement(clock);
 
        node1.setLocation(80, 380);
        node1.setName("equipment 1");
        node1.setSelected(true);
        node1.putLabelPosition(TWaverConst.POSITION_TOP);
        node1.putAttachmentPosition(TWaverConst.POSITION_RIGHT);
        node1.putAttachmentOrientation(TWaverConst.ORIENTATION_NORTH);
        node1.putAttachmentYOffset(20);
        network.getDataBox().addElement(node1);

        node2.setLocation(350, 300);
        node2.setName("equipment 2");
        node2.putAttachmentPosition(TWaverConst.POSITION_INNER_TOPRIGHT);
        node2.putAttachmentOrientation(TWaverConst.ORIENTATION_WEST);
        node2.putAttachmentYOffset(-10);
        box.addElement(node2);

        link.putLinkFlowingColor(Color.RED);
        link.putLinkWidth(6);
        link.putLinkFlowingWidth(6);
        link.putLink3D(true);
        box.addElement(link);

        dial.setLocation(100, 150);
        dial.setName(null);
        dial.addAttachment("dial");
        box.addElement(dial);
        
		Thread thread = new Thread(){
			public void run(){
				while(true){
                    try {
                        Thread.sleep(30);
                        if(network.getCanvas().isShowing()){
                        	changeState1();
                        }
                    }catch (Exception ex) {
                    }
				}
			}
		};
		thread.start();
		
        thread = new Thread() {
            public void run() {
                while (true) {
                    for (step2 = 20; step2 > 0; step2--) {
                        try {
                            Thread.sleep(500);
                            if(network.getCanvas().isShowing()){
                            	changeState2();
                            }
                        }catch (Exception ex) {
                        }
                    }
                }
            }
        };
        thread.start();

	}
	public void changeState1() throws Exception{
		SwingUtilities.invokeAndWait(new Runnable() {
			public void run() {
				angle = dial.getChartValue();
				if(flag == true){
					if((angle += step) > 360){
						angle = 360;
						flag = false;
					}
				}else{
					if((angle -= step) < 0){
						angle = 0;
						flag = true;
					}
				}
				dial.putChartValue(angle);
				// change percentage
				thermometer.setPercentage((int)(angle / 360.0 * 100.0));
				// change hot spot
				clock.putClientProperty(Clock.HOTSPOT, new Double(angle / 360.0));
				// change time
				clock.putClientProperty(Clock.CLOCKTIME, new Date());
				clock.setName(new Date().toString());
			}
		});
	}
	
	public void changeState2() throws Exception{
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                // set node1
                final int temperature = step2;
                node1.putClientProperty("temperature", temperature);
                for (int k = 0; k < KPIAttachment.KEYS.length; k++) {
                    String key = KPIAttachment.KEYS[k];
                    node1.putClientProperty(KPIAttachment.KEYS[k], key + ":" + TWaverUtil.getRandomDouble());
                }

                Boolean state = step2 < 10 ? Boolean.TRUE : Boolean.FALSE;

                // set node1
                if (state.booleanValue()) {
                    node1.getAlarmState().increaseNewAlarm(AlarmSeverity.CRITICAL, 1);
                    node1.putTextureFactory(TWaverConst.TEXTURE_GRID_DIAMOND);
                } else {
                    node1.getAlarmState().decreaseNewAlarm(AlarmSeverity.CRITICAL, 1);
                    node1.putTextureFactory(TWaverConst.TEXTURE_LINE_CROSS);
                }
                if(node1.getAlarmState().isEmpty()){
                	node1.putTextureFactory(null);
                }

                // set node2
                if (state.booleanValue()) {
                	node2.putBodyColor(Color.CYAN);
                	node2.putTextureFactory(TWaverConst.TEXTURE_LINE_DIAGONAL);
                }
                node2.putClientProperty("equipment.state", state);
                node2.putClientProperty("equipment.value", step2);
                
                // set link
                link.putLinkFlowing(!state.booleanValue());
            }
        });
	}
 
}